package com.intellij.tfsSupport.commands;

import com.intellij.openapi.util.text.LineTokenizer;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: lloix
 * Date: May 31, 2006
 */
public class HistoryParser
{
  @NonNls private static final String FILE_SECTION_SIG = "*****";
  @NonNls private static final String VERSION_SECTION_PREFIX_SIG = "*****************  Version ";
  @NonNls private static final String VERSION_SECTION_SUFFIX_SIG = "*****************";
  @NonNls private static final String FOLDER_LABEL_SECTION_PREFIX_SIG = "--------- -------------------- ------------- ---------- -----------------------";
  @NonNls private static final String CHECKED_IN_SIG = "Checked in ";
  @NonNls private static final String FILE_ADDED_SIG = " added";
  @NonNls private static final String FILE_CREATED_SIG = "Created";
  @NonNls private static final String FILE_LABELLED_SIG = "Labeled";
  @NonNls private static final String FILE_DELETED_SIG = " deleted";
  @NonNls private static final String FILE_PURGED_SIG = " purged";
  @NonNls private static final String FILE_RECOVERED_SIG = " recovered";
  @NonNls private static final String FILE_DESTROYED_SIG = " destroyed";

  @NonNls private static final String VERSION_SIG = "Changeset:";
  @NonNls private static final String USER_SIG = "User:";
  @NonNls private static final String DATE_SIG = "Date:";
  @NonNls private static final String TIME_SIG = "Time: ";
  @NonNls private static final String COMMENT_SIG = "Comment:";
  @NonNls private static final String LABEL_COMMENT_SIG = "Label comment: ";
  @NonNls private static final String LABEL_SIG = "Label: ";
  @NonNls private static final String Items_SIG = "Items:";

  private HistoryParser() {}

  public static class SubmissionData
  {
    public String action;
    public String version;
    public String submitter;
    public String changeDate;
    public String comment;
    public String label;
    public int    order;
  }

  public static ArrayList<SubmissionData> parse( final String content )
  {
    ArrayList<SubmissionData> changes = new ArrayList<SubmissionData>();
    String[] lines = LineTokenizer.tokenize( content, false );
    int      order = 0;
    boolean user = false;
    boolean date = false;
      boolean items = false;
      boolean version = false;
      boolean comment = false;
    SubmissionData change = new SubmissionData();
    for( int i = 0; i < lines.length; i++ )
    {
        if(lines[i].indexOf("------")!= -1)
        {
            change = new SubmissionData();
            change.order = order++;
        }
        if((lines[i].indexOf("--------------") == -1 )&&( lines[i] != "")&&(lines[i] != null))
        {

        if(lines[i].indexOf(USER_SIG) != -1 )
        {
            change.submitter = parseActor( lines[i]);
            user = true;
        }
        else if(lines[i].indexOf(DATE_SIG) != -1 )
        {
            change.changeDate = parseDateTime( lines[i]);
            date = true;
        }
        else if(lines[i].indexOf(Items_SIG) != -1 )
        {
            change.action = parseAction(lines[i+1]);
            items = true;
        }
        else if(lines[i].indexOf(VERSION_SIG) != -1 )
        {
            change.version = parseVersion(lines[i]);
            version = true;
        }
        else if(lines[i].indexOf(COMMENT_SIG) != -1 )
        {
            change.comment = parseComment(lines[i+1]);
            comment = true;
        }
        if(user && date && items && version && comment)
        {
            changes.add( change );
            version = false;
            user = false;
            items = false;
            date = false;
            comment = false;
        }
        }
      }
    return changes;
  }

  private static String parseActor( String line)
  {
    String Actor;
    int length = line.length();
    int LengthUser = USER_SIG.length();
        Actor = line.substring( LengthUser, length ).trim();
    return Actor;
  }

  @Nullable
  private static String parseDateTime( String line)
  {
    String Date;
    int length = line.length();
    int LengthDate = DATE_SIG.length();
        Date = line.substring( LengthDate, length ).trim();
    return Date;
  }

  private static String parseVersion( String line)
  {
    String Version;
    int lengthOfVersion = VERSION_SIG.length();
    int lineLength = line.length();
        Version = line.substring( lengthOfVersion, lineLength).trim();
    return Version;
  }

  private static String parseComment( String line)
  {
    String Comment;
    int lengthOfLine = line.length();
        Comment = line.substring(0, lengthOfLine).trim();
    return Comment;
  }
  
  private static String parseAction( String line)
  {
    String action;
    int indexOfPath = line.indexOf("$/");
        action = line.substring(0, indexOfPath).trim();
    return action;
  }
}
